package com.CMUBiS.Depreciated;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.CMUBiS.Data.GlobalVars;
import com.CMUBiS.Data.Header;

@WebServlet(name="ConfirmBidServletold", urlPatterns="/confirmbidold")
public class ConfirmBidServlet extends HttpServlet{
	private static final long serialVersionUID = 6475041003023438743L;
	private static final String EMPTY = "";
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		StringBuffer errorString = new StringBuffer();
		Map<String, String[]> paramMap = request.getParameterMap();
		
		if(!paramMap.isEmpty()){
			//TODO: we should check they are valid
			// Check the userID is present, we need to pass this around
			if(!paramMap.containsKey("userID") || request.getParameter("userID").equals(EMPTY)){
					errorString.append("You must provide a userID. <br />");
			}

			// Check authToken
			if(!paramMap.containsKey("authToken") || request.getParameter("authToken").equals(EMPTY)){
				errorString.append("You must provide a authToken. <br />");
			}
			
			//response.sendRedirect("./myaccount?userID=" + userID + "&authToken=" + authToken);
		}
		
		// If we get to here then there must be an error
		this.makePage(request, response, errorString.toString());
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @param errorString
	 * @throws ServletException
	 * @throws IOException
	 */
	public void makePage(HttpServletRequest request, HttpServletResponse response, String errorString)  throws ServletException, IOException {
		// We are using PrintWriter to be friendly to the international community. PrintWrite auto-converts coding
		PrintWriter out = response.getWriter();
		
		// Include header
		out.println("<HTML>");
		out.println("<HEAD><TITLE>Confirm Bid</TITLE></HEAD>");
		out.println("<BODY>");
		out.println(errorString);
		
		// Do HTML4 or 5 depending on the global variables
		if(GlobalVars.isHTML4)
			createHTML4(request, response, out);
		else
			createHTML5(request, response, out);
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @param out
	 * @return
	 */
	public HttpServletResponse createHTML4(HttpServletRequest request, HttpServletResponse response, PrintWriter out){
		try{
			// Set the return type
			response.setContentType("text/html");
			
			// Get the URL parameters if any were passed
			Map<String, String[]> paramMap = request.getParameterMap();
			
			String userID="-1";
			String authToken="-1";
			
			if(paramMap.containsKey("userID"))
				userID=request.getParameter("userID");
			if(paramMap.containsKey("authToken"))
				authToken=request.getParameter("authToken");
			
			Header.writeHeader(out, "Confirm", userID, authToken);
			
			out.println("<div id=\"center\">");
			out.println("Thank you for bidding on the item. Plase check back after the auction to see if you have won <br />");
			out.println("<a href=\"./myaccount?userID=" + userID + "&authToken=" + authToken+"\">My account</a>");
			out.println("</div>");	
			
			// Include footer
			out.println("</BODY></HTML>");
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return response;
	}
	
	public HttpServletResponse createHTML5(HttpServletRequest request, HttpServletResponse response, PrintWriter out){
		try{
			
			
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return response;
	}
}
